//
//  WidthdrawTrayView.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/9.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class WidthdrawTrayView: BaseView {

    var alipayBlock : ()->Void = {
        
    }
    var wxBlock : ()->Void = {
        
    }
    
    private var blurView : UIView?
    private var opaueView : UIView?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    func popTray()->Void{
        opaueView = UIView.init()
        opaueView?.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: self.width(), height: YXDeviceHeight - 560 * YXHeightScale))
        opaueView?.backgroundColor = UIColor.init(white: 0, alpha: 0.5)
        self.addSubview(opaueView!)
        
        blurView = UIView.init()
        blurView?.backgroundColor = UIColor.white
        blurView!.frame = CGRect.init(origin: CGPoint.init(x: 0, y: self.bounds.size.height), size: CGSize.init(width: self.bounds.size.width, height: 560 * YXHeightScale))
        self.addSubview(blurView!)
        
        let label = UILabel.init()
        label.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: blurView!.width(), height: 100 * YXHeightScale))
        label.text = "选择提现方式"
        label.font = UIFont.systemFont(ofSize: 36 * YXWidthScale)
        label.textAlignment = .center
        blurView?.addSubview(label)
        
        let line = UIView.init()
        line.frame = CGRect.init(x: 0, y: label.height(), width: blurView!.width(), height: YXHeightScale)
        line.backgroundColor = UIColor.colorWithHexString(hex: "ececec")
        blurView!.addSubview(line)
        let param1 = ["image":"icon_choice_pay","title":"支付宝支付","desc":"推荐有支付宝帐号的用户使用"]
        configWidthDrawView(params: param1, idx: 0,superView: blurView!)
        
        let param2 = ["image":"icon_choice_wechat","title":"微信支付","desc":"推荐安装微信5.0及以上版本的用户使用"]
        configWidthDrawView(params: param2, idx: 1,superView: blurView!)
        
        let button = UIButton.init(type: .system)
        button.setTitleColor(UIColor.black, for: .normal)
        button.setTitle("取消选择", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 36 * YXWidthScale)
        button.backgroundColor = UIColor.white
        button.addTarget(self, action: #selector(popSelf), for: .touchUpInside)
        button.frame = CGRect.init(origin: CGPoint.init(x: 0, y: blurView!.bounds.size.height - 120 * YXHeightScale), size: CGSize.init(width: self.width(), height: 120 * YXHeightScale))
        blurView!.addSubview(button)
        UIView.beginAnimations("popUp", context: nil)
        UIView.setAnimationDuration(0.25)
        blurView!.frame = CGRect.init(origin: CGPoint.init(x: 0, y: self.bounds.size.height - 560 * YXHeightScale), size: CGSize.init(width: self.bounds.size.width, height: 560 * YXHeightScale))
        UIView.commitAnimations()
    }
    
    
    @objc func popSelf() -> Void {
        self.removeFromSuperview()
    }
    
    func configWidthDrawView(params : Dictionary<String,String>,idx : Int,superView : UIView)->Void{
        let image = UIImageView.init()
        //60
        let originY = (idx == 0 ? 155 * YXHeightScale : 325 * YXHeightScale)
        image.frame = CGRect.init(x: 48 * YXWidthScale, y: originY, width: 60 * YXWidthScale, height: 60 * YXHeightScale)
        image.image = UIImage.load(name: params["image"])
        superView.addSubview(image)
        
        let lblOriginY = (idx == 0 ? 150 * YXHeightScale : 320 * YXHeightScale)
        let label = UILabel.init()
        let size = "支付宝支付".StringSize(font: UIFont.systemFont(ofSize: 28 * YXWidthScale))
        label.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        label.frame = CGRect.init(x: 128 * YXWidthScale, y: lblOriginY, width: size.width, height: size.height)
        label.textColor = UIColor.colorWithHexString(hex: "2c2c2c")
        label.text = params["title"]
        superView.addSubview(label)
        
        let lbl = UILabel.init()
        let size1 = params["desc"]?.StringSize(font: UIFont.systemFont(ofSize: 20 * YXWidthScale))
        lbl.frame = CGRect.init(x: 128 * YXWidthScale, y: label.bottom() + 10 * YXHeightScale, width: size1!.width, height: size1!.height)
        lbl.textColor = UIColor.colorWithHexString(hex: "909090")
        lbl.font = UIFont.systemFont(ofSize: 20 * YXWidthScale)
        lbl.text = params["desc"]
        superView.addSubview(lbl)
        
        let imageView = UIImageView.init()
        imageView.frame = CGRect.init(origin: CGPoint.init(x: YXDeviceWidth - 48 * YXWidthScale - 24 * YXWidthScale, y: image.bottom() - image.height() / 2 - 20 * YXHeightScale), size: CGSize.init(width: 24 * YXWidthScale, height: 40 * YXHeightScale))
        imageView.image = UIImage.load(name: "icon_enter")
        superView.addSubview(imageView)
        
        let line1 = UIView.init()
        line1.backgroundColor = UIColor.colorWithHexString(hex: "ececec")
        let btm = idx == 0 ? 270 * YXHeightScale : 440 * YXHeightScale
        line1.frame = CGRect.init(origin: CGPoint.init(x: lbl.left(), y: btm), size: CGSize.init(width: 10000, height: YXHeightScale))
        superView.addSubview(line1)
        
        let button = UIButton.init(type: .system)
        var rect : CGRect?
        if idx == 0{
            rect = CGRect.init(origin: CGPoint.init(x: 0, y: 100 * YXHeightScale), size: CGSize.init(width: superView.width(), height: 170 * YXHeightScale))
        }else{
            rect = CGRect.init(origin: CGPoint.init(x: 0, y: 270 * YXHeightScale), size: CGSize.init(width: superView.width(), height: 170 * YXHeightScale))
        }
        button.frame = rect!
        let img = UIImage.image(color: UIColor.clear, size: rect!.size)
        button.setBackgroundImage(img, for: .normal)
        let img1 = UIImage.image(color: UIColor.init(white: 0, alpha: 0.3), size: rect!.size)
        button.setBackgroundImage(img1, for: .highlighted)
        button.tag = 1000 + idx
        button.addTarget(self, action: #selector(controlPush(btn:)), for: .touchUpInside)
        superView.addSubview(button)
        
    }
    
    @objc func controlPush(btn : UIButton)->Void{
        if btn.tag == 1000 {
            alipayBlock()
        }else{
            wxBlock()
        }
        self.removeFromSuperview()
    }
    
    deinit {
        print("WidthDrawTrayView被释放")
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
